<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-button type="success" @click="addBook">添加书籍</el-button>
            </div>
            <div>
                <el-table :data="book" border fit>
                    <el-table-column label="书籍封面" align="center">
                        <template slot-scope="scope">
                            <img :src="ip+scope.row.img" style="width:50%;height:50%" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="书籍名称" align="center">
                    </el-table-column>
                    <el-table-column prop="createTime" label="发布时间" align="center">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button @click="put(scope.row)">编辑</el-button>
                            <el-button @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page="num" layout="total,  prev, pager, next, jumper" :total="totalElements">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { books, delBook, bookTypes } from '../../api/server.js'
import { mapActions } from 'vuex'
import { ip } from '../../utils/local.js'

export default {
    data() {
        return {
            book: [],
            num: 1,
            totalElements: 10,
            ip: ip
        }
    },
    created() {
        this.getBooks(1, 10)
        this.getBookTypes()
    },
    methods: {
        ...mapActions(["clearBook", 'saveBook', 'saveBookTypes']),
        addBook() {
            this.clearBook()
            this.$router.push({
                path: '/editBook'
            })
        },
        handleCurrentChange(val) {
            this.getBooks(val, 10)
        },
        put(row) {
            this.saveBook(row)
            this.$router.push({
                path: '/editBook'
            })
        },
        del(id) {
            console.log('id', id)
            delBook({ id })
                .then(res => {
                    this.$message.info('删除成功')
                    this.getBooks(1, 10)
                })
        },
        getBooks(index, page) {
            books({ index, page })
                .then(res => {
                    this.book = res.result
                    this.totalElements = res.resultNum
                })
        },
        getBookTypes() {
            bookTypes(1, 100)
                .then(res => {
                    this.saveBookTypes(res.result)
                })
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>